﻿using EcoSystem.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace EcoSystem
{
    public class AttachmentAppService : CrudAppService<
        Attachment,
        AttachmentDto,
        Guid,
        GetAttachmentInput,
        CreateOrUpdateAttachmentDto,
        CreateOrUpdateAttachmentDto>, IAttachmentAppService
    {
        public AttachmentAppService(
            IRepository<Attachment, Guid> repository
            ) : base(repository)
        {

        }


        public override async Task<PagedResultDto<AttachmentDto>> GetListAsync(GetAttachmentInput input)
        {
            var query = (await Repository.GetQueryableAsync()).AsNoTracking()
                   .WhereIf(!input.BussinessType.IsNullOrWhiteSpace(), x => x.BussinessType.Equals(input.BussinessType))
                   .WhereIf(!input.GroupName.IsNullOrWhiteSpace(), x => x.GroupName.Equals(input.GroupName))
                   .WhereIf(!input.BusinessId.IsNullOrWhiteSpace(), x => x.BusinessId.Equals(input.BusinessId));
            var total = await query.CountAsync();
            var items = await query.OrderBy(x => x.Id).PageBy(input).ToListAsync();
            return new PagedResultDto<AttachmentDto>()
            {
                Items = await MapToGetListOutputDtosAsync(items),
                TotalCount = total
            };
        }

        public async Task<List<AttachmentDto>> GetAllListAsync(GetAttachmentInput input)
        {
            var query = (await Repository.GetQueryableAsync()).AsNoTracking()
                            .WhereIf(!input.BussinessType.IsNullOrWhiteSpace(), x => x.BussinessType.Equals(input.BussinessType))
                            .WhereIf(!input.GroupName.IsNullOrWhiteSpace(), x => x.GroupName.Equals(input.GroupName))
                            .WhereIf(!input.BusinessId.IsNullOrWhiteSpace(), x => x.BusinessId.Equals(input.BusinessId));
            var items = await query.OrderBy(x => x.Id).ToListAsync();
            return await MapToGetListOutputDtosAsync(items);
        }

        public async Task BatchSaveAsync(BatchSaveAttachmentInputDto input)
        {
            var query = (await Repository.GetQueryableAsync());
            await Repository.DeleteAsync(x => x.BusinessId == input.BusinessId && x.BussinessType == input.BussinessType, true);
            var entities = input.Attachments.Select(x =>
              {
                  var entity = new Attachment()
                  {
                      BusinessId = input.BusinessId,
                      BussinessType = input.BussinessType,
                      GroupName = x.GroupName,
                      FileId = x.FileId,
                      Name = x.Name,
                      FileType = x.FileType,

                  };
                  EntityHelper.TrySetId(entity, () => GuidGenerator.Create());
                  return entity;
              }).ToList();
            await Repository.InsertManyAsync(entities, true);
        }
    }
}
